package com.example.demo.controller;

import com.example.demo.dto.TableFieldDto;
import com.example.demo.service.BaseService;
import com.example.demo.util.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Description: mp控制器
 * CreateDate:  2020/9/24 9:54
 *
 * @author zero
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/table")
public class TestController {

    private final BaseService baseService;

    public TestController(BaseService baseService) {
        baseService.setTableName("t_task002");
        this.baseService = baseService;
    }


    @GetMapping("create")
    public void createNewTable(){
        List<TableFieldDto> cloums = new ArrayList<>();
        for (int i=1;i<5;i++){
            TableFieldDto dto = new TableFieldDto();
            dto.setFieldName("C_0"+i);
            dto.setFieldType("varchar");
            dto.setFieldLength("50");
            dto.setRemark("猪"+i);
            cloums.add(dto);
        }
        TableFieldDto dto = new TableFieldDto();
        dto.setFieldName("C_0"+5);
        dto.setFieldType("double");
        dto.setFieldLength("10,2");
        dto.setRemark("牛");
        cloums.add(dto);
        baseService.createTable(cloums);
    }

    @GetMapping("exist")
    public int exist(){
        return baseService.existTable();
    }

    @GetMapping("drop")
    public int drop(){
        return baseService.dropTable();
    }

    @PostMapping("insert")
    public Object insert(@RequestBody Map<String,Object> map){
        return baseService.insert(map);
    }

    @PostMapping("update")
    public Object update(@RequestBody Map<String,Object> map){
        return baseService.updateByPrimaryKey(map);
    }

    @PostMapping("remove/{id}")
    public Object update(@PathVariable("id") String id){
        return baseService.removeById(id);
    }

    @PostMapping("empty")
    public Object update(){
        return baseService.clearTable();
    }

    @GetMapping("selectOne")
    public Object query(){
        List<String> columns = Arrays.asList("C_01","C_02","C_03","C_05");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.queryColumns(columns)
                .eq("C_01",4)
                .gt("C_02","1")
                .likeLeft("C_03",3)
                .ne("C_04",6);
        return baseService.selectOne(wrapper);
    }

    @GetMapping("selectList")
    public Object list(){
        List<String> columns = Arrays.asList("C_01","C_02","C_03","C_05");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.queryColumns(columns)
                .eq("C_01",4)
                .ne("C_02","100")
                .between("C_05",1,5)
                .in("C_05",Arrays.asList(6,7,8))
                .orderByAsc("C_01","C_02")
                .orderByDesc("C_03");
        return baseService.selectList(wrapper);
    }

}